<?php

Class LoginAction extends Action {

    /**
     * 登录页面展示
     */
    public function index() {
        $this->assign('refer', $_GET['refer']);
        $this->display();
    }

    /**
     * 处理登陆数据
     */
    public function loginHandle() {
        // dump($_POST);
        if (!$_POST||$_POST['username']==''||$_POST['password']=='')
            $this->error('请输入用户名和者密码.');
        $members = M(members)->where(array('username' => $_POST['username']))->find();
        //echo md5($_POST['password']);
        // dump($members);die;
        if ($members) {
            $password = md5($_POST['password']);
            if ($password == $members['password']) {
                //session('username',$members['username']);
                //
				Session::set('username', $members['username']);
                Session::set('uid', $members['uid']);
                Session::set('purview', $members['purview']);


                import("@.ORG.Client");

                $i = new Client();
                $j = $i->getDevice();
                
            // echo $j;                exit();
              
                if ($j == 'mobile') {

                    Session::set('agent', 'mobile');
                } else {

                    Session::set('agent', 'pc');
                }

             

                //M(members)->where(array('username'=>$_POST['username']))->setField('status',1);
                //echo "string";
                // echo "<script type='text/javascript'>window.history.go(-2);window.location.reload(); </script>"; 
                $refer = $_POST['refer'];
                if ($refer == "http://www.mcooz.com/") {
                    $refer = "http://www.mcooz.com/c/i";
                }
                $arr = explode('/', $refer);
                //print_r($arr);                                       exit();
                if (count($arr) < 6) {
                    $this->redirect(U('/' . $arr[3] . '/' . $arr[4]), array(), 2, ' 登录成功!,2秒钟后页面跳转中 ~');
                } else {
                    $this->redirect(U('/' . $arr[3] . '/' . $arr[4] . '/' . $arr[5] . '/' . $arr[6]), array(), 2, ' 登录成功!,2秒钟后页面跳转中 ~');
                }


                //$refer=$_POST['refer'];
                //header('Location:'.$refer);
                //$this->success('login.....',U('/Index/index'));
            } else {
                $this->error('密码错误!.');
            }
        } else {
            $this->error('用户名错误!.');
        }
    }

    /**
     * app登录操作接口
     */
    public function login_act_app() {
        
          if($_POST['username']==''||$_POST['password']==''){
            
              $userArray = array(
                'status' => 19,
                'message' => '请输入用户名或者密码',
            );
    
            
        } else{
            
    

    


        $members = M(members)->where(array('username' => $_POST['username']))->find();

        if ($members) {
            $password = md5($_POST['password']);
            if ($password == $members['password']) {
                Session::set('username', $members['username']);
                Session::set('uid', $members['uid']);
                Session::set('purview', $members['purview']);

                $userArray = array(
                    'token' => md5($members['username'] . "," . $members['password']) . "," . $members['uid'],
                    'status' => 1,
                    'vip' => $members['vip'],
                    'username' => $members['username'],
                    'user_id' => $members['uid'],
                    'message' => '登录成功',
                );

                import("@.ORG.Client");

                $i = new Client();
                $j = $i->getDevice();
                
            // echo $j;                exit();
              
                if ($j == 'mobile') {

                    Session::set('agent', 'mobile');
                } else {

                    Session::set('agent', 'pc');
                }




                //$refer=$_POST['refer'];
                //header('Location:'.$refer);
                //$this->success('login.....',U('/Index/index'));
            } else {
                $userArray = array(
                    'status' => 9,
                    'message' => '密码有误',
                );
            }
        } else {
            $userArray = array(
                'status' => 10,
                'message' => '用户名有误',
            );
        }
        
         }

        $res = json_encode($userArray);

        echo $res;
    }

    /**
     * app退出登录操作
     */
    public function logout_app() {
        //M(members)->where(array('username'=>session('username')))->setField('status',0);
        Session::clear();


        $userArray = array(
            'status' => 11,
            'message' => '退出成功',
        );

        $res = json_encode($userArray);

        echo $res;
    }

    /**
     * 退出登录操作
     */
    public function logout() {
        //M(members)->where(array('username'=>session('username')))->setField('status',0);
        Session::clear();

        $refer = $_POST['refer'];
        $arr = explode('/', $refer);
        if (count($arr) < 6) {
            $this->redirect(U('/' . $arr[3] . '/' . $arr[4]), array(), 2, ' 安全退出!,2秒钟后页面跳转中 ~');
        } else {
            $this->redirect(U('/' . $arr[3] . '/' . $arr[4] . '/' . $arr[5] . '/' . $arr[6]), array(), 2, ' 安全退出!,2秒钟后页面跳转中 ~');
        }
        //$this->redirect(U('/index/index'), array(), 2,' 安全退出!,2秒钟后页面跳转中 ~') ;
        //$this->success('logouting...',U(GROUP_NAME.'/Login/index'));
    }

    /**
     * 用户注册表单处理
     */
    public function registerHandle() {
        // dump($_POST);
        if (empty($_POST)||$_POST['username']==''||$_POST['password']=='')
            $this->error('请输入用户名和密码');
        if (!$_POST && empty($_POST['username']) && empty($_POST['password'])) {
            $this->error('warning....');
        } else {


            $_POST['username'] = trim($_POST['username']);
            $password = md5(trim($_POST['password']));
            // echo $password;die;
            $data = array('nicheng' => $_POST['nicheng'], 'password' => $password, 'username' => $_POST['username']);
            $register = M(members)->data($data)->add();
            if ($register) {

                Session::set('username', $_POST['username']);
                Session::set('uid', $register);

                //print_r($register);exit();

                $refer = $_POST['refer'];
                $arr = explode('/', $refer);
                if (count($arr) < 6) {
                    $this->redirect(U('/' . $arr[3] . '/' . $arr[4]), array(), 2, ' 注册成功!,2秒钟后页面跳转中 ~');
                } else {
                    $this->redirect(U('/' . $arr[3] . '/' . $arr[4] . '/' . $arr[5] . '/' . $arr[6]), array(), 2, ' 注册成功!,2秒钟后页面跳转中 ~');
                }
                // $this->redirect(U('Member/'), array(), 2, ' 注册成功!,2秒钟后页面跳转中 ~');
            } else {
                $this->error('操作失败');
            }
        }
    }

    /**
     * app用户注册表单处理
     */
    public function reg_act_app() {
        // dump($_POST);
        if (empty($_POST))
            $userArray = array(
                'status' => 11,
                'message' => '请输入用户名和密码',
            );

        if (!$_POST && empty($_POST['username']) ||empty($_POST['password'])) {
            $userArray = array(
                'status' => 12,
                'message' => '请输入用户名和密码',
            );
        } else {


            $_POST['username'] = trim($_POST['username']);
            $password = md5(trim($_POST['password']));
            // echo $password;die;
            $data = array('nicheng' => $_POST['nicheng'], 'password' => $password, 'username' => $_POST['username']);
            $register = M(members)->data($data)->add();
            if ($register) {

                Session::set('username', $_POST['username']);
                Session::set('uid', $register);

                //print_r($register);exit();

                $userArray = array(
                    'token' => md5($_POST['username'] . "," . $password) . "," . $register,
                    'status' => 8,
                    'username' => $_POST['username'],
                    'user_id' => $register,
                    'message' => '注册成功',
                );

                // $this->redirect(U('Member/'), array(), 2, ' 注册成功!,2秒钟后页面跳转中 ~');
            } else {
                $userArray = array(
                    'status' => 9,
                    'message' => '注册失败',
                );
            }
        }

        $res = json_encode($userArray);

        echo $res;
    }

}

?>